package yxy.game.pm2.command.temple;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TempleMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.temple.TempleHandle;

import java.util.HashMap;

/** 置换神殿 置换神殿目标武将 */
@MessageType(proto = Command.TempleTargetHeros, classtype = TempleMessage.TempleTargetHerosRequest.class)
public class CMD_TempleTargetHeros extends Command<TempleMessage.TempleTargetHerosRequest> {

	@Override
	public void execute(MessageSource messageSource, TempleMessage.TempleTargetHerosRequest request) {
		String playerCode = messageSource.getTag();
		String heroCode = request.getHero();

		TempleMessage.TempleTargetHerosResponse.Builder response = TempleMessage.TempleTargetHerosResponse.newBuilder();

		
		Hero hero = new HeroHandle().getHero(playerCode, heroCode);
		if (hero == null) {
			send(messageSource, response, Result.HeroNotExists);
			return;// 武将不存在
		}
		TempleHandle templeHandle = new TempleHandle();
		response.addAllHeros(templeHandle.templeTargetHeros(playerCode, hero));

		//自动计算出本体的数量
		HashMap<String, Integer> map = templeHandle.getNum(hero);
		response.setNeedNum(map.get("num"));

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, TempleMessage.TempleTargetHerosResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}